home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / ALG6.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  109 lines

  1. /**************************************************************************
  2.  *
  3.  * alg6.cpp - STL generic algorithms that produce new sequences
  4.  *    section 12.7
  5.  *
  6.  * $Id: alg6.cpp,v 1.3 1995/08/29 18:43:29 oberg Exp $
  7.  *
  8.  * $$RW_INSERT_HEADER "slyrs.cpp"
  9.  *
  10.  **************************************************************************/
  11.  
  12. # include <vector>
  13. # include <list>
  14. # include <algorithm>
  15. # include <numeric>
  16.  
  17. # include <iostream.h>
  18. using namespace std;
  19.  
  20. int square(int n) { return n * n; }
  21.  
  22. class iotaGen {
  23. public:
  24.     iotaGen (int iv) : current(iv) { }
  25.     operator () () { return current++; }
  26. private:
  27.     int current;
  28. };
  29.  
  30.  
  31. void transform_example ()
  32.     // illustrate the use of the transform algorithm
  33. {
  34.     // generate a list of values from 1 to 6
  35.     list<int> aList;
  36.     generate_n (inserter(aList, aList.begin()), 6, iotaGen(1));
  37.     cout << "Original list: ";
  38.     copy(aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  39.     
  40.         // transform elements by squaring, copy into vector
  41.     vector<int> aVec(6);
  42.     transform (aList.begin(), aList.end(), aVec.begin(), square);
  43.     cout << "After squaring: ";
  44.     copy(aVec.begin(), aVec.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  45.     
  46.         // transform vector again, in place, yielding 4th powers
  47.     transform (aVec.begin(), aVec.end(), aVec.begin(), square);
  48.     cout << "After squaring again: ";
  49.     copy(aVec.begin(), aVec.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  50.     
  51.         // transform in parallel, yielding cubes
  52.     vector<int> cubes(6);
  53.     transform (aVec.begin(), aVec.end(), aList.begin(),
  54.         cubes.begin(), divides<int>());
  55.     cout << "After division: ";
  56.     copy(cubes.begin(), cubes.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  57.     
  58. }
  59.  
  60. void partial_sum_example ()
  61.     // illustrate the use of the partial sum algorithm
  62. {
  63.         // generate values 1 to 5
  64.      vector<int> aVec(5);
  65.      generate (aVec.begin(), aVec.end(), iotaGen(1));
  66.     
  67.          // output partial sums
  68.      cout << "Partial sums examples" << endl;
  69.      cout << "Partial sums : ";
  70.      partial_sum (aVec.begin(), aVec.end(),
  71.          ostream_iterator<int>(cout, " ")), cout << endl;
  72.          
  73.          // output partial products
  74.      cout << "Partial products: ";
  75.      partial_sum (aVec.begin(), aVec.end(),
  76.          ostream_iterator<int>(cout, " "), times<int>() ), cout << endl;
  77. }
  78.  
  79. void adjacent_difference_example ()
  80.     // illustrate the use of the adjacent difference algorithm
  81. {
  82.         // generate values 1 to 5
  83.      vector<int> aVec(5);
  84.      generate (aVec.begin(), aVec.end(), iotaGen(1));
  85.     
  86.          // output partial sums
  87.      cout << "Adjacent Differences examples" << endl;
  88.      cout << "Adjacent Differences : ";
  89.     adjacent_difference (aVec.begin(), aVec.end(),
  90.          ostream_iterator<int>(cout, " ")), cout << endl;
  91.          
  92.          // output partial products
  93.      cout << "Adjacent sums: ";
  94.      adjacent_difference (aVec.begin(), aVec.end(),
  95.          ostream_iterator<int>(cout, " "), plus<int>() ), cout << endl;
  96. }
  97.  
  98.  
  99. int main() {
  100.     cout << "STL generic algorithms -- that transform sequences"  << endl;
  101.     
  102.     transform_example();
  103.     partial_sum_example();
  104.     adjacent_difference_example ();
  105.     
  106.     cout << "End generic transform algorithms example" << endl;
  107.     return 0;
  108. }
  109.